package javaSE;

//***********************************************************************************************************
//--------------------------------------------方法------------------------------------------

import java.util.Scanner;

public class Demo2 {
    /*public static  int sumAdd(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum = sum + i;
        }
        return sum;
    }
        public static void main1(String[] args) {
            int ret = sumAdd(10);//方法的调用
            System.out.println(ret);
            //System.out.println(sumAdd(10));  此时函数必须要有返回值   函数的返回值支持链式调用
        }*/


    //******************输入密码登录成功********************
    public static void login() {
        Scanner scanner = new Scanner(System.in);
        int count = 3;
        while (count != 0) {
            System.out.println("请输入你的密码：");
            String password = scanner.nextLine();
            if (password.equals("123456")) {             //比较字符串
                System.out.println("登录成功了");
                break;
            } else {
                count--;
                System.out.println("你输错密码了，你还有" + count + "次机会");
            }
        }
    }

    public static void main2(String[] args) {
        login();
    }


    //*************水仙花数*************************
    public static void findNum(int n) {
        for (int i = 1; i <= n; i++) {
            int temp = i;
            int count = 0;//数字的位数
            while (temp != 0) {
                count++;
                temp /= 10;
            }
            temp = i;
            int sum = 0;
            while (temp != 0) {
                sum += Math.pow(temp % 10, count);
                temp /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }

    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        findNum(n);
    }


    //***************计算一个二进制数中1的个数****************
    public static int numofone(int n) {
        int count = 0;
        while (n != 0) {
            if ((n & 1) == 1) {//按位与             count++;n =n & (n-1);每次按位与都会少一个一，最后等于0说明1就没有了
                count++;
            }
            n = n >>> 1;//无符号右移一位
        }
        return count;
    }

    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个二进制数：");
        int n = scanner.nextInt();
        System.out.println("此二进制数中1的个数为：" + numofone(n));
    }


    //***************求出一个二进制数的奇偶序列****************
    public static void printOne(int n) {
        System.out.println("所得偶数序列为：");
        for (int i = 30; i >= 0; i -= 2) {
            System.out.print(((n >> i) & 1) + "");
        }
        System.out.println();
        System.out.println("所得奇数序列为：");
        for (int i = 31; i >= 1; i -= 2) {
            System.out.print(((n >> i) & 1) + "");
        }
    }

    public static void main5(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个二进制数：");
        int n = scanner.nextInt();
        System.out.println();
        printOne(n);
    }


    //*************输出整数的每一位*****************************
    public static void print(int n) {
        //逆序打印
        while (n != 0) {
            System.out.print(n % 10 + "");
            n /= 10;
        }
    }

    public static void main6(String[] args) {
        print(123);
    }


    //****************打印图形********************************
    public static void func(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    System.out.print("*");
                } else if (i + j == n - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    public static void main7(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个数：");
        int n = scanner.nextInt();
        func(n);
    }


    //******************找出出现一次的数字***********************
    public static void main8(String[] args) {
        int[] array = {1, 2, 3, 2, 1};
        int sum = array[0];
        for (int i = 1; i < array.length; i++) {
            sum = sum ^ array[i];
        }
        System.out.println("所得的数为：");
        System.out.println(sum);
    }


    //*****************递归思想:一个方法在执行过程中调用自身   必须先理解清楚 "方法的执行过程", 尤其是 "方法执行结束
//                                                  之后, 回到调用位置继续往下执行
    public static int sumAdd(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n + sumAdd(n - 1);
        }
    }

    public static void main9(String[] args) {
        System.out.println(sumAdd(5));
    }

    public static int sumEveryOne(int n) {
        if (n < 10) {
            return n;
        } else {
            return sumEveryOne(n / 10) + n % 10;
        }
    }

    public static void main10(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个数：");
        int n = scanner.nextInt();
        System.out.println("所得结果为：");
        System.out.println(sumEveryOne(n));
    }


    //*****************汉诺塔思想：2^n - 1********************
    public static void move(char pos1, char pos3) {//执行最大盘子n的从pos1-pos2的移动
        System.out.print(pos1 + "->" + pos3 + " ");
    }

    public static void hannuota(int n, char pos1, char pos2, char pos3) {  //n:盘子的数量 pos1:起始位置 pos2:中转位置 pos3:结束位置
        if (n == 1) {
            move(pos1, pos3);
        } else {
            hannuota(n - 1, pos1, pos3, pos2);
            move(pos1, pos3);
            hannuota(n - 1, pos2, pos1, pos3);
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入移动盘子的数量和对应的位置：");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        char pos1 = scanner.next().charAt(0);//       输入一个字符
        char pos2 = scanner.next().charAt(0);
        char pos3 = scanner.next().charAt(0);
        System.out.println("所得移动顺序是：");
        hannuota(n, pos1, pos2, pos3);
        System.out.println();
    }
}






